package com.xu.mylol.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xu.mylol.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/12/29.
 */
public class MyRecyclerViewAdapter_Goods extends RecyclerView.Adapter<MyRecyclerViewAdapter_Goods.MyViewHolder>{
    private Context context;
    private List<String> data;
    public MyOnClickListener myOnClickListener;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public MyRecyclerViewAdapter_Goods(Context context) {
        this.context = context;
        initdata();
    }

    private void initdata() {
        data = new ArrayList<>();

        for (int i = 0; i <1000 ; i++) {
            data.add("物品资料"+i);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.recycler_item_goods,null);
        MyViewHolder myViewHolder = new MyViewHolder(v);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.textView.setText(data.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myOnClickListener != null) {
                    myOnClickListener.onMyClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void addItem(int position,String s){
        data.add(position,s);
        notifyDataSetChanged();
    }
    public void deleteItem(int position){
        data.remove(position);
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_recycleView_review_goods);
        }
    }
}
